package com.ruoyi.rms.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.rms.domain.MemberRecharge;
import com.ruoyi.rms.enums.MemberStatus;
import com.ruoyi.rms.enums.RechargeStatus;
import com.ruoyi.rms.service.IMemberRechargeService;
import com.ruoyi.rms.utils.IdUtils;
import com.ruoyi.rms.utils.RetUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rms.mapper.MemberMessageMapper;
import com.ruoyi.rms.domain.MemberMessage;
import com.ruoyi.rms.service.IMemberMessageService;

/**
 * 会员信息Service业务层处理
 * 
 * @author wyl
 * @date 2024-03-11
 */
@Service
@Slf4j
public class MemberMessageServiceImpl implements IMemberMessageService 
{
    @Autowired
    private MemberMessageMapper memberMessageMapper;

    @Autowired
    private IMemberRechargeService memberRechargeService;

    private final BigDecimal MAX_PRICE = BigDecimal.valueOf(10000.00);
    private final BigDecimal DIS_COUNT = BigDecimal.valueOf(0.85);

    /**
     * 查询会员信息
     * 
     * @param memberId 会员信息主键
     * @return 会员信息
     */
    @Override
    public MemberMessage selectMemberMessageByMemberId(Long memberId)
    {
        return memberMessageMapper.selectMemberMessageByMemberId(memberId);
    }

    /**
     * 根据微信号查询会员信息
     * @param wxId
     * @return
     */
    @Override
    public MemberMessage getInfoByWxId(String wxId) {
        return memberMessageMapper.getInfoByWxId(wxId);
    }

    /**
     * 查询会员信息列表
     * 
     * @param memberMessage 会员信息
     * @return 会员信息
     */
    @Override
    public List<MemberMessage> selectMemberMessageList(MemberMessage memberMessage)
    {
        return memberMessageMapper.selectMemberMessageList(memberMessage);
    }

    /**
     * 新增会员信息
     * 
     * @param memberMessage 会员信息
     * @return 结果
     */
    @Override
    public int insertMemberMessage(MemberMessage memberMessage)
    {
//        if (overMaxPrice(memberMessage)) {
//            return RetUtils.ERROR;
//        }

        String wxId = memberMessage.getWxId();
        if (wxId == null || repeatWxId(wxId)) {
            return RetUtils.ERROR;
        }

        if (memberMessage.getMaxPrice() == null) memberMessage.setMaxPrice(MAX_PRICE);
        if (memberMessage.getDiscount() == null) memberMessage.setDiscount(DIS_COUNT);
        if (memberMessage.getPrice() == null) memberMessage.setPrice(BigDecimal.ZERO);
        memberMessage.setMemberId(IdUtils.createId());
        memberMessage.setCreateTime(DateUtils.getNowDate());
        memberMessage.setStatus(MemberStatus.ENABLE.getStatus()); // 默认为启用状态
        return memberMessageMapper.insertMemberMessage(memberMessage);
    }

    /**
     * 修改会员信息
     * 
     * @param memberMessage 会员信息
     * @return 结果
     */
    @Override
    public int updateMemberMessage(MemberMessage memberMessage)
    {
        if (overMaxPrice(memberMessage)) {
            return RetUtils.ERROR;
        }

        memberMessage.setUpdateTime(DateUtils.getNowDate());
        return memberMessageMapper.updateMemberMessage(memberMessage);
    }

    /**
     * 批量删除会员信息
     * 
     * @param memberIds 需要删除的会员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberMessageByMemberIds(Long[] memberIds)
    {
        return memberMessageMapper.deleteMemberMessageByMemberIds(memberIds);
    }

    /**
     * 删除会员信息信息
     * 
     * @param memberId 会员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberMessageByMemberId(Long memberId)
    {
        return memberMessageMapper.deleteMemberMessageByMemberId(memberId);
    }

    /**
     * 充值
     * @param memberId
     * @param price
     * @return
     */
    @Override
    public int recharge(Long memberId, BigDecimal price) {
        if (BigDecimal.ZERO.compareTo(price) >= 0) return RetUtils.ERROR;
        MemberMessage memberMessage = memberMessageMapper.selectMemberMessageByMemberId(memberId);
        if(memberMessage == null) return RetUtils.ERROR;

        BigDecimal initPrice = memberMessage.getPrice();
        if (MAX_PRICE.compareTo(price.add(initPrice)) < 0) {
            log.error("超过最大限额，充值失败");
            return RetUtils.ERROR;
        }

        memberMessage.setPrice(price.add(initPrice));
        memberMessageMapper.updateMemberMessage(memberMessage); // 修改余额

        String wxId = memberMessage.getWxId();
        String phone = memberMessage.getPhone();

        MemberRecharge memberRecharge = new MemberRecharge();
        if (wxId != null) memberRecharge.setWxId(wxId);
        if (phone != null) memberRecharge.setPhone(phone);
        memberRecharge.setPrice(price);
        memberRecharge.setStatus(RechargeStatus.RECHARGE.getStatus());
        return memberRechargeService.insertMemberRecharge(memberRecharge); // 添加充值记录
    }

    /**
     * 判断是否超出限额
     * @param memberMessage
     * @return
     */
    private Boolean overMaxPrice(MemberMessage memberMessage) {
        Long memberId = memberMessage.getMemberId();
        BigDecimal price = memberMessage.getPrice();
        MemberMessage message = memberMessageMapper.selectMemberMessageByMemberId(memberId);
        if (message == null) {
            // 新注册的会员
            if (MAX_PRICE.compareTo(price) < 0) {
                log.error("超过最大限额，充值失败");
                return true;
            }
        } else {
            BigDecimal initPrice = message.getPrice(); // 原先的余额
            if (MAX_PRICE.compareTo(price.add(initPrice)) < 0) {
                log.error("超过最大限额，充值失败");
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否重复注册会员
     * @param wxId
     * @return
     */
    private Boolean repeatWxId(String wxId) {
        MemberMessage memberMessage = memberMessageMapper.getInfoByWxId(wxId);
        return memberMessage != null;
    }
}
